﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using MegavaniaX.InputProcessor;
using MegavaniaX.Rendering;
using Microsoft.Xna.Framework.Graphics;
using MegavaniaX.World;
using MegavaniaX.Data.Hardcoded;
using MegavaniaX.Music;
using MegavaniaX.UI;
namespace MegavaniaX.States
{
    public class MainGameState : IState
    {
        private IInputProcessor _inputProcessor;
        private MegavaniaCamera _camera;
        private MusicManager _musicManager;
        private UIManager _uiManager;

        public bool IsPaused
        {
            get { return false; }
        }


        public MainGameState(Microsoft.Xna.Framework.Content.ContentManager contentManager)
        {

            _camera = new MegavaniaCamera() { gameHeight = 768, gameWidth = 768, gameX = 0, gameY = 512, ZoomFactor = 3.0f };
            MessagingSystem.SubscribeToEvent(_camera.OnCameraMoveStart, MegavaniaEventType.CameraMoveUpStart, "camera");
            MessagingSystem.SubscribeToEvent(_camera.OnCameraMoveStop, MegavaniaEventType.CameraMoveUpFinished, "camera");
            
            _musicManager = new MusicManager(contentManager);
            _inputProcessor = new DefaultInputProcessor();

            MapManager.Camera = _camera;
            _uiManager = new UIManager(contentManager);
            

            #region temp delete me!

            MegavaniaX.logger.Debug("Loading content.");





            // Add a fake Player actor.
            Player tempPlayerActor = new Player("player");
            tempPlayerActor.position.X = 40;
            tempPlayerActor.position.Y = 520;

            _uiManager.AddLifeBar(tempPlayerActor);

            // add an eye block dude
            EyeBlock tempEyeBlock = new EyeBlock("eyeblock");
            tempEyeBlock.position.X = 100;
            tempEyeBlock.position.Y = 400;

            EyeBlock tempEyeBlock2 = new EyeBlock("eyeblock2");
            tempEyeBlock2.position.X = 200;
            tempEyeBlock2.position.Y = 400;

            // add nodes
            AINode eb1Node1 = new AINode("eb1Node1");
            eb1Node1.position.X = 100;
            eb1Node1.position.Y = 400;

            AINode eb1Node2 = new AINode("eb1Node2");
            eb1Node2.position.X = 300;
            eb1Node2.position.Y = 400;

            AINode eb2Node1 = new AINode("eb2Node1");
            eb2Node1.position.X = 200;
            eb2Node1.position.Y = 400;

            AINode eb2Node2 = new AINode("eb2Node2");
            eb2Node2.position.X = 300;
            eb2Node2.position.Y = 300;

            // add nodes to eyeblock
            tempEyeBlock.SetNodes(new TimedAINode(eb1Node1, new TimeSpan(0, 0, 0, 0, 2000)),
                                  new TimedAINode(eb1Node2, new TimeSpan(0, 0, 0, 0, 5000)));
            tempEyeBlock.SetNodes(new TimedAINode(eb2Node1, new TimeSpan(0, 0, 0, 0, 200)),
                                  new TimedAINode(eb2Node2, new TimeSpan(0, 0, 0, 0, 200)));


            // Add event subscriptions.
            /*
            MessagingSystem.SubscribeToEvent(tempPlayerActor.MoveRightPressed, MegavaniaEventType.MoveRightPressed, "player");
            MessagingSystem.SubscribeToEvent(tempPlayerActor.MoveRightReleased, MegavaniaEventType.MoveRightReleased, "player");
            MessagingSystem.SubscribeToEvent(tempPlayerActor.MoveLeftPressed, MegavaniaEventType.MoveLeftPressed, "player");
            MessagingSystem.SubscribeToEvent(tempPlayerActor.MoveLeftReleased, MegavaniaEventType.MoveLeftReleased, "player");
            MessagingSystem.SubscribeToEvent(tempPlayerActor.OnJumpPressed, MegavaniaEventType.JumpPressed, "player");
            MessagingSystem.SubscribeToEvent(tempPlayerActor.OnJumpReleased, MegavaniaEventType.JumpReleased, "player");
            MessagingSystem.SubscribeToEvent(tempPlayerActor.OnSlidePressed, MegavaniaEventType.SlidePressed, "player");
            MessagingSystem.SubscribeToEvent(tempPlayerActor.OnShootPressed, MegavaniaEventType.ShootPressed, "player");
            MessagingSystem.SubscribeToEvent(tempPlayerActor.ChangeWeapon, MegavaniaEventType.WeaponChangeStarted, "player");
            */
            MessagingSystem.SubscribeToEvent(tempPlayerActor.HandleEvent, MegavaniaEventType.JumpPressed, "player");
            MessagingSystem.SubscribeToEvent(tempPlayerActor.HandleEvent, MegavaniaEventType.JumpReleased, "player");
            MessagingSystem.SubscribeToEvent(tempPlayerActor.HandleEvent, MegavaniaEventType.MoveRightPressed, "player");
            MessagingSystem.SubscribeToEvent(tempPlayerActor.HandleEvent, MegavaniaEventType.MoveRightReleased, "player");
            MessagingSystem.SubscribeToEvent(tempPlayerActor.HandleEvent, MegavaniaEventType.MoveLeftPressed, "player");
            MessagingSystem.SubscribeToEvent(tempPlayerActor.HandleEvent, MegavaniaEventType.MoveLeftReleased, "player");
            MessagingSystem.SubscribeToEvent(tempPlayerActor.HandleEvent, MegavaniaEventType.SlidePressed, "player");
            MessagingSystem.SubscribeToEvent(tempPlayerActor.OnShootPressed, MegavaniaEventType.ShootPressed, "player");
            MessagingSystem.SubscribeToEvent(tempPlayerActor.HandleEvent, MegavaniaEventType.WeaponChangeStarted, "player");
            MessagingSystem.SubscribeToEvent(MapManager.StartClimbing, MegavaniaEventType.ClimbPressed, "player");

            // world manager event subscriptions
            MessagingSystem.SubscribeToEvent(MapManager.OnSpawnActor, MegavaniaEventType.SpawnActor, "world");
            MessagingSystem.SubscribeToEvent(MapManager.OnDespawnActor, MegavaniaEventType.DespawnActor, "world");

            MessagingSystem.SubscribeToEvent(_camera.ChangeZoom, MegavaniaEventType.CameraZoomChange, "camera");

            MapManager.CurrentMap = contentManager.Load<Map>("levels/shadowzone");
            MapManager.CurrentMap.Actors.Add(tempPlayerActor);
            MapManager.CurrentMap.Actors.Add(tempEyeBlock);
            MapManager.CurrentMap.Actors.Add(tempEyeBlock2);
            MapManager.CurrentMap.CurrentScreen = MapManager.CurrentMap.Screens[0];
            _musicManager.PlayMusic("music/whitetiger");

            #endregion





        }

        public void Update(GameTime gameTime)
        {
            // Process all events in the queue
            MessagingSystem.ProcessQueued();
            MapManager.Update(gameTime.ElapsedGameTime);

            // Process input
            _inputProcessor.ProcessInput(Keyboard.GetState(), Mouse.GetState(), gameTime.ElapsedGameTime);
        }

        public void Render(SpriteBatch spriteBatch)
        {
            MapManager.Draw(spriteBatch, _camera);
            _uiManager.Draw(spriteBatch);
            //MapManager.DrawActors(spriteBatch, _camera);
        }

        public void ProcessInput(KeyboardState keyState, MouseState mouseState, TimeSpan elapsedTime)
        {
            _inputProcessor.ProcessInput(keyState, mouseState, elapsedTime);
        }
    }
}
